<?php
/**
 * @Author: Marte
 * @Date:   2018-03-24 14:21:36
 * @Last Modified by:   Marte
 * @Last Modified time: 2018-03-29 10:14:46
 */
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\News;
use Illuminate\Support\Facades\Session;

class NewsController extends Controller{

        // 信息列表页
        public function news(){
            $news = News::paginate(10);
            // dd($news);
            // 把session类传到页面供其使用
            $session = new session();
            return view('news.index',['news_res'=>$news,'session'=>$session]);
        }

        // 信息新增页
        public function create(Request $request){
            //$this是指当前的控制器
            // 如果是post提交
            if($request->isMethod('post')){
                // 1.控制器验证表单数据
                // $this->validate($request,['News.title'=>'required|min:2|max:8','News.content'=>'required|min:2|max:50','News.is_show'=>'required|integer'],['required'=>':attribute 为必填项'],['News.title'=>'标题','News.content'=>'内容']);

                // 2.validate类验证
                $validate_res = \Validator::make($request->input(),['News.title'=>'required|min:2|max:8','News.content'=>'required|min:2|max:50','News.is_show'=>'required|integer'],['required'=>':attribute 为必填项','min'=>':attribute 长度不符'],['News.title'=>'标题','News.content'=>'内容','News.is_show'=>'是否显示']);

                // withInput()//数据保持，不用传参，默认已经把$request传入,在模板中对应的input的value={{old('News')['name']}}，将旧原来输入的值存入session中
                // withErrors验证失败时将错误信息存入session的errors数组中
                //
                // [errors] => Illuminate\Support\ViewErrorBag Object ( [bags:protected] => Array ( [default] => Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [News.title] => Array ( [0] => 标题 为必填项 ) [News.content] => Array ( [0] => 内容 为必填项 ) ) [format:protected] => :message ) ) ) [_old_input] => Array ( [_token] => 9QFf3riQqrnfTU3IN6a716QxbEG1veDjukV3OFlO [News] => Array ( [title] => [content] => [is_show] => 0 ) ) )
                if($validate_res->fails()){

                    return redirect()->back()->withErrors($validate_res)->withInput();
                }


                // 获取提交过来的数据
                $data = $request->input('News');
                // 批量赋值到各个字段并保存，新增成功会返回模型本身
                if(News::create($data)){
                    return redirect('news')->with('success','添加成功');
                }else{
                    return redirect()->back()->with('error','添加失败');
                }
            }
            return view('news.create');
        }

        // 信息新增表单
        public function save(Request $request){
            if ($request->isMethod('post')) {
                $data = $request->input('News');//name名用数组的形式
                $news = new News();
                $news->title = $data['title'];
                $news->content = $data['content'];
                $news->is_show = $data['is_show'];
                if($news->save()){
                    return redirect('news')->with('success','添加成功');
                }else{
                    return redirect()->back()->with('error','添加失败');
                }
            }
        }


        // 更新
        public function update(Request $request,$id){
            // $id获取修改传来的id
            // 查找到对应的数据
            $news = News::find($id);

            if($request->isMethod('post')){
                 $validate_res = \Validator::make($request->input(),['News.title'=>'required|min:2|max:8','News.content'=>'required|min:2|max:50','News.is_show'=>'required|integer'],['required'=>':attribute 为必填项','min'=>':attribute 长度不符'],['News.title'=>'标题','News.content'=>'内容','News.is_show'=>'是否显示']);
                 if($validate_res->fails()){
                    return redirect()->back()->withErrors($validate_res)->withInput();
                 }
                // 获取提交来的数据
                $data = $request->input('News');
                // 修改数据
                $news->title = $data['title'];
                $news->content = $data['content'];
                $news->is_show = $data['is_show'];
                if($news->save()){
                    return redirect('news')->with('success','修改成功-'.$id);
                }else{
                    return redirect()->back()->with('error','修改失败-'.$id);
                }


            }
            return view('news.update',['news'=>$news]);
        }

        // 删除
        public function delete($id){
            $news = News::find($id);

            if($news->delete()){
                return redirect('news')->with('success','删除成功-'.$id);
            }else{
                return redirect()->back()->with('error','删除失败-'.$id);
            }
        }


}